#include "NanoVirtualIO.h"
#include "string.h"

#define TAG "NanoVirtualIO"
#include "elog.h"
#define NANO_LIGD(tag,...)  elog_d(tag,__VA_ARGS__)
#define NANO_LOGI(tag,...)  elog_i(tag,__VA_ARGS__)
#define NANO_LOGW(tag,...)  elog_i(tag,__VA_ARGS__)
#define NANO_LOGE(tag,...)  elog_i(tag,__VA_ARGS__)
#define NANO_HEXDUMP(tag,buf,len)   elog_hexdump(tag,16,buf,len)

NanoVirtualIO::NanoVirtualIO(const char* name ) :  NanoIO( "virtual io" )
{
    _isOpen = false;
    strcpy( _name , name );
    NANO_LOGI(TAG,"Create virtual io ,name:\"%s\"." , name );
}

NanoVirtualIO::~NanoVirtualIO()
{
    if( _isOpen )
    {
        close();
    }
}

bool NanoVirtualIO::open()
{
    _isOpen = true;
    return true;
}

bool NanoVirtualIO::close()
{
    _isOpen = false;
    return true;
}

bool NanoVirtualIO::isOpen()
{
    return _isOpen;
}

int NanoVirtualIO::read(uint8_t* data , uint16_t len , uint32_t timeout_ms)
{
    (void)data;
    (void)len;
    (void)timeout_ms;
    return 0;
}

int NanoVirtualIO::write(uint8_t* data , uint16_t len , uint32_t timeout_ms)
{
    (void)data;
    (void)len;
    (void)timeout_ms;
    if( _isOpen )
    {
        NANO_LOGI(TAG,"IO(name:%s) write %d bytes:",this->_name,len);
        NANO_HEXDUMP(_name,data,len);
        return len;
    }
    NANO_LOGW(TAG,"IO(name:%s) write failed,io is closed." , this->_name );
    return -1;
}
